package com.example.esp8266.Library.RoomLib;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.esp8266.R;

import java.util.List;

public class TaskAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_TASK = 0; // 任务视图类型
    private static final int VIEW_TYPE_NEW_TASK = 1; // 新建任务视图类型

    private List<Task> tasks;
    private Context context;
    private OnTaskEditListener onTaskEditListener;
    private OnTaskCreateListener onTaskCreateListener;

    public TaskAdapter(List<Task> tasks, Context context) {
        this.tasks = tasks;
        this.context = context;
    }

    // 添加点击事件监听器的接口
    public interface OnTaskEditListener {
        void onTaskEdit(Task task);
    }

    // 添加新任务按钮点击事件监听器的接口
    public interface OnTaskCreateListener {
        void onCreateNewTask();
    }

    public void setOnTaskEditListener(OnTaskEditListener listener) {
        this.onTaskEditListener = listener;
    }

    public void setOnTaskCreateListener(OnTaskCreateListener listener) {
        this.onTaskCreateListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        // 根据位置返回不同的视图类型
        if (position < tasks.size()) {
            return VIEW_TYPE_TASK;
        } else {
            return VIEW_TYPE_NEW_TASK;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_TASK) {
            // 加载任务项布局
            View view = LayoutInflater.from(context).inflate(R.layout.task_item, parent, false);
            return new TaskViewHolder(view);
        } else { // VIEW_TYPE_NEW_TASK
            // 加载新建任务项布局
            View view = LayoutInflater.from(context).inflate(R.layout.new_task_item, parent, false);
            return new NewTaskViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder.getItemViewType() == VIEW_TYPE_TASK) {
            // 绑定任务数据到视图
            Task task = tasks.get(position);
            TaskViewHolder taskViewHolder = (TaskViewHolder) holder;
            taskViewHolder.taskNameTextView.setText(task.taskName);

            // 设置编辑按钮的点击事件监听器
            taskViewHolder.taskEdit.setOnClickListener(v -> {
                if (onTaskEditListener != null) {
                    onTaskEditListener.onTaskEdit(task);
                }
            });

        } else {
            NewTaskViewHolder newTaskViewHolder = (NewTaskViewHolder) holder;

            newTaskViewHolder.taskCreateBtn.setOnClickListener(v -> {
                if (onTaskCreateListener != null) {
                    onTaskCreateListener.onCreateNewTask();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        // 返回项数，如无任务则返回1以显示新增任务项
        return !tasks.isEmpty() ? tasks.size() + 1 : 1; // +1 表示新增任务项
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setTasks(List<Task> tasks) {
        this.tasks = tasks;
        notifyDataSetChanged();
    }

    static class TaskViewHolder extends RecyclerView.ViewHolder {
        TextView taskNameTextView;
        ImageView taskEdit;

        public TaskViewHolder(View itemView) {
            super(itemView);
            taskNameTextView = itemView.findViewById(R.id.task_name);
            taskEdit = itemView.findViewById(R.id.task_edit);
            // 确保只试图初始化 task_item.xml 文件中的控件
        }
    }

    static class NewTaskViewHolder extends RecyclerView.ViewHolder {
        ImageView taskCreateBtn;

        public NewTaskViewHolder(View itemView) {
            super(itemView);
            taskCreateBtn = itemView.findViewById(R.id.task_create_btn);
            // 确保只试图初始化 new_task_item.xml 文件中的控件
        }
    }
}